<?php
class yunxin {
	
	public function __construct(){
		//请求地址
		$this->url = 'https://api.netease.im/sms/sendtemplate.action';
		$this->key = 'e1e0e0a2e4e1c5d3efdb9d99818cbdd1';
		$this->secret = '79ba78b976dd';
		
		//验证码地址
		//https://api.netease.im/sms/verifycode.action
		
	}
	

	//发送短信
	public function send_sms($params){
		$data = '';
		if(is_array($params)){
			foreach($params as $k=>$v){
				$data .= '&'.$k.'='.$v;
			}
		}else{
			die('请求错误');
		}
		$data = substr($data,1);
		$secret = $this->secret;
		//当前时间
		$time = time();
		
		$url = $this->url;
		
		//随机字符串
		$rstr = $this->getRandChar(10);
				
		$checkSum = sha1($secret.$rstr.$time);

		//头信息
		$header = array();
		$header[] = "AppKey: $this->key";
		$header[] = "CurTime: $time";
		$header[] = "CheckSum: $checkSum";
		$header[] = "Nonce: $rstr";
		$header[] = 'charset: utf-8';
		$header[] = 'Content-Type: application/x-www-form-urlencoded';

		//初始化curl
		$ch = curl_init ();

		curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$result = curl_exec($ch);  
		$error = curl_error($ch);
		curl_close($ch);
	
	}

	//获取随机字符串
	public function getRandChar($length){
		$str = '';
		$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		$max = strlen($strPol)-1;

		for($i=0;$i<$length;$i++){
			$str.=$strPol[rand(0,$max)];
		}

		return $str;
	}

}

